package com.ruoyi.iotcommon.utils;

public class IotdbSqlUtils {

    /** 句号 */
    private static final char POINT = '.';

    public static String selectList(String tableName, long startTime, long endTime, int limit){
        return String.format(
                "SELECT * FROM %s WHERE  time >= %d AND time <= %d ORDER BY time DESC LIMIT %d",
                tableName,
                startTime,
                endTime,
                limit
        );
    }


    public static String selectList(String tableName,String propertyKey, long startTime, long endTime, int limit){
        return String.format(
                "SELECT %s FROM %s WHERE  time >= %d AND time <= %d ORDER BY time DESC LIMIT %d",
                propertyKey,
                tableName,
                startTime,
                endTime,
                limit
        );
    }

    public static String selectLast(String tableName){
        return String.format("SELECT LAST * FROM %s ",tableName);
    }

    public static String selectLast(String tableName,String propertyKey){
        return String.format("SELECT LAST %s FROM %s ",propertyKey,tableName);
    }


    /**
     * 分页查询
     * @param tableName
     * @param pageNum
     * @param pageSize
     * @param startTime
     * @param endTime
     * @return
     */
    public static String selectPage(String tableName,int pageNum, int pageSize, long startTime, long endTime){
        return String.format(
                "SELECT * FROM %s WHERE time >= %d AND time <= %d LIMIT %d OFFSET %d",
                tableName,
                startTime,
                endTime,
                pageSize,
                (pageNum - 1) * pageSize
        );
    }

    public static String selectPage(String tableName,String propertyKey,int pageNum, int pageSize, long startTime, long endTime){
        return String.format(
                "SELECT %s FROM %s WHERE time >= %d AND time <= %d LIMIT %d OFFSET %d",
                propertyKey,
                tableName,
                startTime,
                endTime,
                pageSize,
                (pageNum - 1) * pageSize
        );
    }

    /**
     * 高性能分页（基于时间戳）
     * @param tableName
     * @param startTime
     * @param endTime
     * @param lastTimestamp
     * @param pageSize
     * @return
     */
    public static String selectPageByTimestamp(
            String propertyKey,
            String tableName,
            long startTime,
            long endTime,
            long lastTimestamp, // 上次查询的最后时间戳
            int pageSize
    ){
        return String.format(
                "SELECT %s FROM %s WHERE time >= %d AND time <= %d AND time > %d ORDER BY time ASC LIMIT %d",
                tableName, startTime, endTime, lastTimestamp, pageSize
        );
    }
    /**
     * 统计数据条数，可用于计算总页数
     * @param tableName
     * @param startTime
     * @param endTime
     * @return
     */
    public static String selectCount(String tableName, long startTime, long endTime){
        return String.format("SELECT count(*) FROM %s WHERE  time >= %d AND time <= %d",tableName, startTime, endTime);
    }

    public static String buildTableName(String productKey,String channelKey,String deviceKey){
        StringBuffer tableName = new StringBuffer();
        tableName.append("root.db");
        //tableName.append(业务标识); // 可以增加业务标识，比如项目id，分组id，租户id，等，不同业务数据分离，查询更快
        //tableName.append(POINT);
        tableName.append(POINT);
        tableName.append(productKey);
        tableName.append(POINT);
        tableName.append(deviceKey);
        tableName.append(POINT);
        tableName.append(channelKey);
        return tableName.toString();
    }
}
